from django.shortcuts import render,HttpResponse,redirect,reverse
from django.core.paginator import Paginator
from .models import *
# Create your views here.
def commodity_add(request):
    if request.method == 'GET':
        return render(request,'addd.html')
    if request.method == 'POST':
        commodity_name = request.POST.get('commodity_name')
        date_of_manufacture = request.POST.get('date_of_manufacture')
        type_of_merchandise = request.POST.get('type_of_merchandise')
        address = request.POST.get('address')

        q1 = Commodity(
            commodity_name=commodity_name,
            date_of_manufacture=date_of_manufacture,
            type_of_merchandise=type_of_merchandise,
            address=address
        )
        q1.save()
        result = '添加成功'
        return render(request,'addd.html',{'result':result})

def showinfo(request):
    c_show = Commodity.objects.all()
    p = Paginator(c_show,5)
    x= request.GET.get('page',1)
    try:
        x = int(x)
        if x < 0:
            x = 1
    except Exception as e:
        x = 1
    pagex = p.get_page(x)
    return render(request, 'showinfo.html', {'pagex': pagex})
def showinfo(request):
    c_show = Commodity.objects.all()
    return render(request, 'showinfo.html', {'pagex': c_show})

def showinfoi(request):
    c_show = Commodity.objects.all()
    return render(request, 'showinfoi.html', {'pagex': c_show})

def Pay(request,id):
    a = Commodity.objects.filter(id=id)

    return render(request,'pay.html',{'pay':a})


def Order_form_add(request,id):
    Order_form.objects.filter(id=id)
    if request.method == 'GET':
        return render(request, 'pay.html')
    if request.method == 'POST':
        order_number = request.POST.get('order_number')
        shipping_address = request.POST.get('shipping_address')
        message_img = request.FILES.get('message_img')
        name = request.POST.get('name')
        phone = request.POST.get('phone')
        q2 = Order_form(
            order_number=order_number,
            shipping_address=shipping_address,
            message_img=message_img,
            name=name,
            phone=phone
        )
        q2.save()
        print(request.POST)
        result = '购买成功'
        return render(request,'payover.html',{'result':result})

def c_del(request,id):
    s2 = Commodity.objects.filter(id=id).delete()
    return redirect(reverse('id:showinfoi'))